import Vue from 'vue'
import Router from 'vue-router'
import tabRoutes from './tabRoutes.js'
import signRoutes from './signRoutes.js'
import userRoutes from './userRoutes.js'
import answerRoutes from './answerRoutes.js'
import syRoutes from './syRoutes.js'
import inviteRoutes from './inviteRoutes.js'
import discountRoutes from './discountRoutes.js'
import store from "@/store"
import Axios from '@/utils/pending'

Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
const routes = [{
        path: "/",
        redirect: "/home",
        meta: {
            keepAlive: false
        }
    },
    {
        name: 'dou',
        path: '/dou',
        component: () =>
            import ("@/views/dou/index.vue"),
        meta: {}
    },
    {
        name: 'notice',
        path: '/notice',
        component: () =>
            import ("@/views/notice/index.vue"),
        meta: {}
    }, {
        name: 'notice_detail',
        path: '/notice_detail/:id',
        component: () =>
            import ("@/views/notice/detail.vue"),
        meta: {}
    },
    {
        name: 'search',
        path: '/search',
        component: () =>
            import ("@/views/search"),
        meta: {}
    },
    {
        name: 'product',
        path: '/product/:id',
        component: () =>
            import ("@/views/product/product.vue")
    },
    {
        name: 'product_comment_list',
        path: '/comment/:goodsId',
        component: () =>
            import ("@/views/product/comment.vue")
    },
    {
        name: 'super_product',
        path: '/super_product/:id',
        component: () =>
            import ("@/views/product/super_product.vue")
    },
    {
        name: 'help',
        path: '/help',
        component: () =>
            import ("@/views/help"),
        meta: {}
    },
    {
        name: 'merchant',
        path: '/merchant',
        component: () =>
            import ("@/views/merchant"),
        meta: {}
    },
    {
        name: 'download',
        path: '/download',
        component: () =>
            import ("@/views/download"),
        meta: {}
    },
    {
        name: 'wx_code',
        path: '/wx_code',
        component: () =>
            import ("@/views/wx_code"),
        meta: {}
    },
    {
        name: 'kefu',
        path: '/kefu',
        component: () =>
            import ("@/views/kefu"),
        meta: {}
    },
    {
        name: 'contact',
        path: '/contact',
        component: () =>
            import ("@/views/contact"),
        meta: {}
    },
    {
        name: 'step',
        path: '/step',
        component: () =>
            import ("@/views/step"),
        meta: {}
    },
    {
        name: 'phb',
        path: '/phb/:index/:show',
        component: () =>
            import ("@/views/phb"),
        meta: {}
    }
].concat(tabRoutes).concat(signRoutes).concat(userRoutes).concat(answerRoutes).concat(syRoutes).concat(inviteRoutes).concat(discountRoutes)
const router = new Router({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes
})


// 是否需要登录
router.beforeEach((to, from, next) => {
    store.state.uid = localStorage.getItem('uid'); //获取本地存储的uid
    store.state.isRz = localStorage.getItem('isRz'); //获取本地存储的isRz
    var oToken = store.state.token = JSON.parse(localStorage.getItem("token")); //获取本地存储的token
    store.state.aeskey = localStorage.getItem('aeskey'); //获取本地存储的aeskey
    Axios.clearPending();
    if (to.name === 'login' && store.state.uid) {
        next({
            path: '/my'
        })
        return;
    }
    if (to.name === 'login' && !store.state.uid) {
        next()
        return;
    } else {
        if (to.meta.requiresAuth && (!oToken || !oToken.t || !oToken.e || (oToken.e + 2592000000 < new Date().getTime()))) {
            to.query.active = 0;
            next({
                path: '/login',
                query: to.query,
                params: to.params
            })
        } else {
            //判断是否需要缓存
            // next()
            const noCaches = ['free_sy', 'discount_sy', 'home', 'my'];
            if (['product', 'super_product'].includes(from.name) && ["free_sy", "discount_sy"].includes(to.name)) {
                if (!to.meta.keepAlive) {
                    to.meta.keepAlive = true
                }
                next();
            } else if (noCaches.includes(to.name)) {
                to.meta.keepAlive = false
                next()
            } else {
                next()
            }
        }
    }
})
router.afterEach((to, from, next) => {
    window.scrollTo(0, 0)
    if (from.name == 'login') {
        window.location.reload();
    }
})
export default router